﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Hybrid;
using System.Windows.Forms;

namespace Hybrid
{
    class Sprite
    {
        private int privateSpeedX;
        private int privateSpeedY;
        private int privateX;
        private int privateY;
        private int privateWidth;
        private int privateHeight;
        private List<Image> privateSpriteMap;
        private int privateAnimationIndex = 0;
        private bool privateStartAnimation = false;
        private bool privateDestroyed = false;
        private int maxWidth = 730;
        private int maxHeight = 570;
        private Image blankImage = Image.FromFile(Application.StartupPath + "\\Images\\blank.png");

        public Sprite(List<Image> SpriteMap, int PositionX, int PositionY, int SpeedX, int SpeedY, bool centerX, bool correctHeightY)
        {
            privateSpriteMap = SpriteMap;
            privateX = PositionX;
            privateY = PositionY;
            privateWidth = privateSpriteMap[0].Width;
            privateHeight = privateSpriteMap[0].Height;
            privateSpeedX = SpeedX;
            privateSpeedY = SpeedY;
            if (centerX) privateX -= privateSpriteMap[0].Width / 2;
            if (correctHeightY) privateY -= privateSpriteMap[0].Height;

        }

        public Image Image
        {
            get
            {
                if (privateStartAnimation)
                {
                    if (privateAnimationIndex < 3) privateAnimationIndex++;
                    else
                    {
                        privateDestroyed = true;
                        return blankImage;
                    }
                }
                return privateSpriteMap[privateAnimationIndex];
            }
        }

        public bool DestroyStarted
        {
            get
            {
                return privateStartAnimation;
            }
        }

        public void StartDestroy()
        {
            privateStartAnimation = true;
        }

        public void Reset()
        {
            privateStartAnimation = false;
            privateDestroyed = false;
            privateAnimationIndex = 0;
        }

        public bool Destroyed
        {
            get
            {
                return privateDestroyed;
            }
        }

        public bool CollisionDetected(Sprite SO)
        {
            if ((SO.X + SO.Width > privateX && SO.X < privateX + privateWidth) && (SO.Y + SO.Height > privateY && SO.Y < privateY + privateHeight))
            {
                return true;
            }
            else return false;
        }

        public bool InBounds(int side)
        {
            if (side == 0)
            {
                if (privateX > 0) return true;
                else return false;
            }
            else if (side == 1)
            {
                if (privateX + privateWidth < maxWidth) return true;
                else return false;
            }
            else if (side == 2)
            {
                if (privateY > 0) return true;
                else return false;
            }
            else if (side == 3)
            {
                if (privateY + privateHeight < maxHeight) return true;
                else return false;
            }
            else return false;
        }

        public int CenterX
        {
            get
            {
                return privateX + (privateWidth / 2);
            }
        }

        public int X
        {
            get
            {
                return privateX;
            }
            set
            {
                privateX = value;
            }
        }

        public int Y
        {
            get
            {
                return privateY;
            }
            set
            {
                privateY = value;
            }
        }

        public int NewX
        {
            get
            {
                privateX += privateSpeedX;
                return privateX;
            }
        }

        public int NewY
        {
            get
            {
                privateY += privateSpeedY;
                return privateY;
            }
        }

        public int SpeedX
        {
            get
            {
                return privateSpeedX;
            }
            set
            {
                privateSpeedY = value;
            }
        }

        public int Width
        {
            get
            {
                return privateWidth;
            }
            set
            {
                privateWidth = value;
            }
        }

        public int Height
        {
            get
            {
                return privateHeight;
            }
            set
            {
                privateHeight = value;
            }
        }
    }
}
